package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.HostModuleData;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClicks;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NHostTaskListAdapter extends RecyclerView.Adapter<NHostTaskListAdapter.HostTaskListViewHolder> {
    private final Context mContext;
    private List<HostModuleData> mTaskList;
    private LayoutInflater layoutInflater;
    private IItemClicks mIItemClick;

    public NHostTaskListAdapter(List<HostModuleData> taskList, Context context, IItemClicks iItemClick) {
        this.mTaskList = taskList;
        if (mTaskList == null) {
            mTaskList = new ArrayList<>();
        }
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.mIItemClick = iItemClick;
    }

    @NonNull
    @Override
    public HostTaskListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View views = layoutInflater.inflate(R.layout.host_task_list_item, viewGroup, false);
        return new HostTaskListViewHolder(views);
    }

    @Override
    public void onBindViewHolder(@NonNull HostTaskListViewHolder hostTaskListViewHolder, final int i) {
        int pos = hostTaskListViewHolder.getAdapterPosition();
        hostTaskListViewHolder.tvTaskName.setText(mTaskList.get(pos).getTaskName());
        hostTaskListViewHolder.imTaskOptions.setOnClickListener(v -> {
            if (mIItemClick != null) {
                mIItemClick.onClick(pos, v);
            }
        });
        hostTaskListViewHolder.itemMain.setOnClickListener(v -> {
            if (mIItemClick != null) {
                mIItemClick.onMainItemClick(pos, v);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    public class HostTaskListViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_task_name)
        TextView tvTaskName;
        @BindView(R.id.im_task_options)
        ImageView imTaskOptions;
        @BindView(R.id.itemMain)
        RelativeLayout itemMain;

        public HostTaskListViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
